package com.person.modules.sys.controller;

import com.person.common.utils.PageUtils;
import com.person.common.utils.R;
import com.person.common.validator.ValidatorUtils;
import com.person.modules.sys.entity.SysCompanyEntity;
import com.person.modules.sys.entity.SysCompanyUserEntity;
import com.person.modules.sys.entity.SysUserEntity;
import com.person.modules.sys.service.SysCompanyService;
import com.person.modules.sys.service.SysUserService;
import com.person.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 注册公司
 */
@Controller
@RequestMapping("/sys/company")
public class SysCompanyController extends AbstractController {
    String overallCompName;
    @Autowired
    SysCompanyService sysCompanyService;
    @Autowired
    SysUserService sysUserService;

    /**
     * 注册
     */
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    /*@RequiresPermissions("sys:company:save")*/
    @CrossOrigin
    public R save(SysCompanyEntity sysCompanyEntity) throws IOException {
        //sysCompanyService.save(sysCompanyEntity);

        //校验类型
        ValidatorUtils.validateEntity(sysCompanyEntity);
        sysCompanyService.register(sysCompanyEntity);
        return R.ok();
    }

    /**
     * 注册  需要传入公司名字和图片
     */
    @ResponseBody
    @RequestMapping(value = "/registerPic",method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    /*@RequiresPermissions("sys:company:save")*/
    @CrossOrigin
    public R registerPic(MultipartFile file) throws IOException {

        //图片上传成功后，将图片的地址写到数据库
        //保存图片的路径（这是存在我项目中的images下了，你们可以设置路径）
        String filePath = "D:\\person_sys\\cache\\BusinessLicense";
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        //新的文件名字
        String newFileName = UUID.randomUUID() + originalFilename;
        //封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        //把本地文件上传到封装上传文件位置的全路径
        file.transferTo(targetFile);

       //sysCompanyService.save(sysCompanyEntity);

        //校验类型
        if (overallCompName!=null){
            sysCompanyService.registerPic(newFileName,overallCompName);
        }
        else{
            return R.error(000,"compNull");
        }
        overallCompName="";
        return R.ok();
    }
    /**
     * 公司未审核列表
     */
    @CrossOrigin
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("sys:company:list")
    public R list() {
        List<SysCompanyEntity> list= sysCompanyService.getCompanyList(0);
        return R.ok().put("SysCompanyEntity",list);
    }

    /**
     * 公司名验证
     * @param companyName
     * @return
     */
    @ResponseBody
    @RequestMapping("/companyVer")
    public R AjaxCompanyVer(@RequestParam(value = "companyName",required = false) String companyName){
        overallCompName = companyName;
        SysCompanyEntity sysCompany = sysCompanyService.queryByCompanyName(overallCompName);
        if (sysCompany==null){
            //公司名可以使用
            return R.ok().put("msg",true);
        }else {
            //公司名已经存在 不能使用该名称
            return R.ok().put("msg",false);
        }
    }

    /**
     * 用户名验证
     * @param userName
     * @return
     */
    @ResponseBody
    @RequestMapping("/userNameVer")
    public R AjaxUserNameVer(@RequestParam(value = "userName",required = false) String userName){
        SysCompanyEntity sysCompany = sysCompanyService.queryByUserName(userName);
        if (sysCompany==null){
            //用户名可以使用
            return R.ok().put("msg",true);
        }else {
            //用户名已经存在 不能使用该名称
            return R.ok().put("msg",false);
        }
    }

    /**
     *  返回当前已经在平台上注册的企业数（超管权限）
     * @return
     */
    @ResponseBody
    @CrossOrigin
    @RequiresPermissions("sys:company:count")
    @RequestMapping("/companyCount")
    public R companyCount(){
        Integer count =sysCompanyService.getCompanyCount();
       return R.ok().put("count",count);
    }

}
